﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FPP.Models;
using System.Text;
using FPP.Custom;
using System.Data.Objects;

namespace FPP.Controllers
{
    public class PartyController : BaseController
    {

        private User _User;

        //
        // GET: /Party/

        public ActionResult Index()
        {
            List<Party> lParty = new List<Party>();

            lParty = context.Parties.OrderBy(p => p.Name).ToList();

            return View(lParty);
        }

        //
        // GET: /Party/Details/5

        public ActionResult Details(int id)
        {
            return View(context.Parties.FirstOrDefault(p => p.Id == id));
        }

        //
        // GET: /Party/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Party/Create

        [HttpPost]
        [CustomAuthorize]
        public ActionResult Create(Party _Party, FormCollection collection)
        {

            if (!ModelState.IsValid) return View(_Party);
          
            fppEntities qContext = new fppEntities(DataAccess.ffpCString);

            try
            {
                // TODO: Add insert logic here
                using (qContext)
                {
                    _User = GetUserFromLoginToken(HttpContext.User.Identity.Name);

                    _Party.OwnerId = _User.Id;
                    _Party.Name = collection["Name"];
                    
                    qContext.Parties.AddObject(_Party);
                    qContext.SaveChanges(SaveOptions.None);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [CustomAuthorize]
        public ActionResult Join(int id)
        {

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var _Party = context.Parties.FirstOrDefault(p => p.Id == id);
                var _user = new User();

                _user = GetUserFromLoginToken(HttpContext.User.Identity.Name);

                if (!_Party.AddUser(_user.Id))
                {
                    return RedirectToAction("Details", new { id = _Party.Id, ErrorMessage = _Party.ErrorMessage });
                }
            }

            return RedirectToAction("Details", new { id = id });
        }

        [CustomAuthorize]
        public ActionResult Delete(int id)
        {

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var _Party = context.Parties.FirstOrDefault(p => p.Id == id);
                var _user = new User();

                _user = GetUserFromLoginToken(HttpContext.User.Identity.Name);
                
                if (!_Party.RemoveUser(_user.Id))
                {
                    return RedirectToAction("Details", new { id = _Party.Id, ErrorMessage = _Party.ErrorMessage });
                }
            }

            return RedirectToAction("Details", new { id = id });
        }

        //
        // GET: /Party/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Party/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
